package com.livelab.java;

import com.livelab.model.ManageAccount;
import com.livelab.util.MySQLDatabaseUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 * Backing bean supports "manageAccount.xhtml"
 *
 * @author Joey
 */
@ManagedBean
@RequestScoped
public class ManageAccountBean {

    private static final String UPDATE = "UPDATE agsstudent SET email = ?, studentLoginEnabled = ?, major = ? WHERE username=?;"; //update statement
    MySQLDatabaseUtil db; //database
    private ArrayList<ManageAccount> manageAccount; //list to populate table
    @ManagedProperty(value = "#{instructorLoginBean}")
    private InstructorLoginBean instructor; //instructor login bean
    private List<SelectItem> selectItems; //list of items to select
    private List<String> selectedItems; //list of selected items

    /**
     * The Constructor
     */
    public ManageAccountBean() {
        db = new MySQLDatabaseUtil("localhost/javalivelab9e", "agsuser", "agsuser");
        manageAccount = new ArrayList<>();
        selectItems = new ArrayList<SelectItem>();

    }

    @PostConstruct
    public void init() {
        ResultSet rs;
        rs = db.executeQuery("SELECT username, firstname, lastname, email, studentLoginEnabled, major FROM agsstudent WHERE agsstudent.courseID = '" + instructor.getCourseId() + "';");
        try {
            //populate the table
            while (rs.next()) {
                manageAccount.add(new ManageAccount(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getBoolean(5), rs.getString(6)));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        rs = db.executeQuery("SELECT firstname, lastname, username FROM agsstudent WHERE agsstudent.courseID = '" + instructor.getCourseId() + "';");
        try {
            //populate the list
            while (rs.next()) {
                selectItems.add(new SelectItem(rs.getString(3), rs.getString(1) + " " + rs.getString(2)));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    /*
     * SETTERS
     */
    public void setSelectItems(List<SelectItem> items) {
        this.selectItems = new ArrayList(items);
    }

    public void setManageAccount(ArrayList<ManageAccount> manageAccount) {
        this.manageAccount = manageAccount;
    }

    public void setInstructor(InstructorLoginBean instructor) {
        this.instructor = instructor;
    }
    
    public void setSelectedItems(List<String> items){
        this.selectedItems = items;
    }

    
    /*
     * GETTERS
     */
    public List<String> getSelectedItems() {
        return selectedItems;
    }

    public List<SelectItem> getSelectItems() {
        return selectItems;
    }

    public ArrayList<ManageAccount> getManageAccount() {
        return manageAccount;
    }

    /**
     *Remove student function
     */
    public void removeStudent() {
        FacesContext context = FacesContext.getCurrentInstance();
        for (String selectedItem : selectedItems) {
            db.executeStatement("DELETE FROM agsstudent WHERE username='" + selectedItem + "';");
            db.executeStatement("DELETE FROM agslog WHERE username='" + selectedItem + "';");
        }
        context.addMessage(null, new FacesMessage("Remove Students", "Success"));
    }

    /**
     * Remove instructor function
     */
    public void removeInstructor() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (selectItems.isEmpty()) {
            db.executeStatement("DELETE FROM agsinstructor WHERE username='" + instructor.getUsername() + "';");
            context.addMessage(null, new FacesMessage("Remove Instructor", "Success"));
        }
        else{
            context.addMessage(null, new FacesMessage("Remove Instructor", "Failed, Students still exist"));
        }
    }

    /**
     * Update Students Function
     */
    public void updateStudents() {
        PreparedStatement ps = db.getPreparedStatement(UPDATE);
        FacesContext context = FacesContext.getCurrentInstance();
        for (ManageAccount e : manageAccount) {
            try {
                ps.setString(1, e.getEmail());
                ps.setBoolean(2, e.getLoginEnabled());
                ps.setString(3, e.getMajor());
                ps.setString(4, e.getUsername());

                ps.executeUpdate();
            } catch (SQLException ex) {
                context.addMessage(null, new FacesMessage("Update Table", "Failure"));
            }
        }
        context.addMessage(null, new FacesMessage("Update Table", "Success"));
    }
}